﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;
namespace CoreLibrary
{
    // this class is for system security setup
    public class CoreSecurity
    {
        // this method is to calculate hash md5 from a string
        public static string getMd5Hash(string inputString)
        {
            MD5 md5hash = MD5.Create();
            byte[] data = md5hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < data.Length; i++ )
            {
                str.Append(data[i].ToString("X2"));
            }
            return str.ToString();
        }
    }

    // base class for people
    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public Person()
        {
            this.Id = 0;
            this.FirstName = string.Empty;
            this.LastName = string.Empty;
            this.Gender = string.Empty;
            this.Address = string.Empty;
            this.Email = string.Empty;
            this.Phone = string.Empty;
        }
    }
    // base class for base object

    public class BaseObject
    {
        public int Id { get; set; }
        private string _name;
        private string _description;
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        public String Name
        {
            get { return this._name;}
            set { this._name = value; }
        }
        public int IsDeleted { get; set; }
        public BaseObject()
        {
            this.Id = 0;
            this.Name = "";
            this.Description = "";
            this.IsDeleted = 0;
        }
    }
    // Comparison class
    public class Comparison
    {
        // compare a time between two other times
        public bool CompareTime(string TimeToCompare, string StartTime,string EndTime)
        {
            bool State = false;
           
            DateTime t1 = Convert.ToDateTime(StartTime);
            DateTime t2 = Convert.ToDateTime(EndTime);
            DateTime t = Convert.ToDateTime(TimeToCompare);
            int h = t.Hour;
            int h1 = t1.Hour;
            int h2 = t2.Hour;
            h = h > 12 ? h - 12 : h;
            h1 = h1 > 12 ? h1 - 12 : h1;
            h2 = h2 > 12 ? h2 - 12 : h2;

            string a1 = t1.ToString("tt");
            string a2 = t2.ToString("tt");
            ///////////////////////////////////////////////////////////
            // AM case
            if(t.ToString("tt")=="AM")
            {
                //if a1 and a2 are AM
                if(a1=="AM" && a2=="AM")
                {
                    // t1 and t2 are in the same day
                    if(h1<=h2)
                    {
                        // compare t between t1 and t2
                        if(h>=h1 && h<=h2)
                        {
                            State = true;
                        }

                    }
                    else
                    {
                        // t2 is the next day
                        // h1=10 am, h2= 3 am
                        // h1 is today, h2 is next day
                        // h2 = h2 + 12
                        
                        ///////////////////////////////////
                        // compare t between t1 and t2
                        if(h>=h1 && h<= h2+12)
                        {
                            State = true;
                        }
                    }
                }
                ////////////////////////////////////////////
                // if a1 is am and a2 is pm
                else if(a1=="AM" && a2=="PM")
                {
                    // compare t between t1 and t2
                    if(h>=h1 && h<=h2+12 && h!=12)
                    {
                        State = true;

                    }
                   
                }
                ///////////////////////////////////////////////////
                // if a1 is pm and a2 is am
                else if(a1=="PM" && a2=="AM")
                {
                    // h1=11 pm, h2 = 4 am
                    // h = 1 am
                    // compare t between t1 and t2
                    if(h+12>=h1 && h<=h2+12)
                    {
                        State = true;
                    }
                }
                ///////////////////////////////////////////////////
                // if a1 and a2 are pm
                else if(a1=="PM" && a2=="PM")
                {
                    // h is am, so h not in range of pm in the same day, except different day
                    
                    // if a1 and a2 in different day
                    // h1=11 pm, h2 = 3 pm (h2 is the next day)
                    // h=1 am
                    if(h1>h2)
                    {
                        // compare t between t1 and t2
                        if(h>=h1-12 && h<=h2+12)
                        {
                            State = true;
                        }
                    }

                }
            }
            ////////////////////////////////////////////
            // PM case
            if(t.ToString("tt")=="PM")
            {
                //if a1 and a2 are AM
                if (a1 == "AM" && a2 == "AM")
                {
                    // h is pm, so h is not in ragne of AM in the same day, except different day

                    // if a1 and a2 in different day
                    // h1=11 am, h2=1 am
                    // h=3 pm
                    if(h1>h2)
                    {
                        // compare t between t1 and t2
                        if(h+12>=h1 && h<=h2+12)
                        {
                            State = true;
                        }
                    }
                }
                ////////////////////////////////////////////////////////
                // if a1 is pm and a2 is am
                else if(a1=="PM" && a2=="AM")
                {
                    // h is pm
                    // a1 and a2 are not in the same day
                    // h1=11 pm, h2=2am
                    // h=12am
                    if(h>=h1 && h<= h2+12)
                    {
                        State = true;
                    }
                }
                /////////////////////////////////////////////////////////
                // if a1 is am and a2 is pm
                else if(a1=="AM" && a2=="PM")
                {
                    // h is pm
                    // if a1 and a2 are in the same day
                    //h1=11am, h2=3pm
                    // h=12pm
                    if(h>=h1 && h<=h2+12)
                    {
                        State = true;
                    }
                }
                //////////////////////////////////////////////////////////
                // if a1 and a2 are pm
                else if(a1=="PM" && a2=="PM")
                {
                    // h is pm
                    // if a1 and a2 are in the same day
                    if(h1<=h2)
                    {
                        if(h>=h1 && h<=h2)
                        {
                            State = true;
                        }
                    }
                    // a1 and a2 are not in the same day
                    else
                    {
                        //h1=10pm, h2=1pm
                        // h =11pm
                        if(h>=h1 && h<=h2+12)
                        {
                            State = true;
                        }
                    }


                }
            }

            return State;
        }    
        
        // other function to compare time, but not work well
        public bool CompTime(DateTime t, DateTime t1, DateTime t2)
        {
            bool State = false;
            
          // if current time is AM
          if (t.ToString("tt") == "AM")
          {
              // if t1 and t2 are AM
              if (t1.ToString("tt") == "AM" && t2.ToString("tt") == "AM")
              {
                  // if t1 and t2 are in the same day
                  if (t1.Ticks < t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
                  // if t1 is the next day
                  else
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks)
                      {
                          State = true;
                      }
                      else
                      {

                          State = false;
                      }
                  }
              }
              // if t1 is AM and t2 is PM
              if (t1.ToString("tt") == "AM" && t2.ToString("tt") == "PM")
              {
                  // t1 and t2 are in the same day
                  if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                  {
                      State = true;
                  }
                  else
                  {
                      State = false;
                  }
              }
              // if t1 is PM and t2 is AM
              if (t1.ToString("tt") == "PM" && t2.ToString("tt") == "AM")
              {
                  // t2 is the next day
                  if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks && t.Hour!=12)
                  {
                      State = true;
                  }
                  else if(t.Ticks==12 && t.Ticks<=t1.Ticks && t.Ticks<=t2.AddDays(1).Ticks)
                  {
                      State = true;
                  }
                  else
                  {
                      State = false;
                  }
              }
              // if t1 and t2 are PM
              if (t1.ToString("tt") == "PM" && t2.ToString("tt") == "PM")
              {
                  // if t1 and t2 are in the same day
                  if (t1.Ticks < t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
                  // if t2 is the next day
                  if (t1.Ticks > t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
              }
          }
          else if (t.ToString("tt") == "PM")
          {
              // if t1 and t2 are PM
              if (t1.ToString("tt") == "PM" && t2.ToString("tt") == "PM")
              {
                  // if t1 and t2 are in the same day
                  if (t1.Ticks < t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
                  // if t2 is in the next day
                  else if (t1.Ticks > t2.Ticks)
                  {
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }

              }
              // if t1 is PM and t2 is AM
              if (t1.ToString("tt") == "PM" && t2.ToString("tt") == "AM")
              {
                  // compare t between t1 and t2
                  if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks)
                  {
                      State = true;
                  }
                  else
                  {
                      State = false;
                  }
              }
              // if t1 is AM and t2 is PM
              if (t1.ToString("tt") == "AM" && t2.ToString("tt") == "PM")
              {
                  // compare t between t1 and t2
                  if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                  {
                      State = true;
                  }
                  else
                  {
                      State = false;
                  }
              }
              // if t1 and t2 are am
              if (t1.ToString("tt") == "AM" && t2.ToString("tt") == "AM")
              {
                  // if t1 and t2 are in the same day
                  if (t1.Ticks < t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
                  else if (t1.Ticks > t2.Ticks)
                  {
                      // compare t between t1 and t2
                      if (t.Ticks >= t1.Ticks && t.Ticks <= t2.AddDays(1).Ticks)
                      {
                          State = true;
                      }
                      else
                      {
                          State = false;
                      }
                  }
              }
          }
           // return the state of the time
          return State;
        }
    }

    // Messaging class
    public class Messaging
    {
        // send E-mail
        public void SendMail(string MailTo, string Subject, string SubjectBody)
        {
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.Host = "184.107.177.146";
            client.EnableSsl = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;

            client.Credentials = new System.Net.NetworkCredential("vongkol@khschools.com", "KhmerCamb0dia");

            MailMessage mm = new MailMessage("vongkol@khschools.com", MailTo, Subject, SubjectBody);
            mm.IsBodyHtml = true;
            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            try
            {
                client.Send(mm);
            }
            catch (Exception ex)
            {

            }
        }

        // send message to phone
        public void SendSms(string PhoneNo, string text)
        {

        }
    }
}
